package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.TransportCatalogRpcService;
import com.hunttown.mes.rpc.domain.TransportCatalogDTO;
import com.hunttown.mes.rpc.domain.query.TransportCatalogDTOQuery;
import com.hunttown.mes.service.TransportCatalogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.TransportCatalogConverts.convert;

/**
 * created by wangjunfu on 2019-05-19 01:20:09
 */
@Service
public class TransportCatalogRpcServiceImpl implements TransportCatalogRpcService {

    private final Logger logger = LoggerFactory.getLogger(TransportCatalogRpcServiceImpl.class);

    private final TransportCatalogService transportCatalogService;

    @Autowired
    public TransportCatalogRpcServiceImpl(TransportCatalogService transportCatalogService) {
        this.transportCatalogService = transportCatalogService;
    }

    @Override
    public TransportCatalogDTO insert(TransportCatalogDTO transportCatalogDTO) {
        try {
            return convert(transportCatalogService.insert(convert(transportCatalogDTO)));
        } catch (Exception e) {
            logger.error("TransportCatalog create failed.", e);
            throw new RuntimeException("TransportCatalog create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(TransportCatalogDTO transportCatalogDTO) {
        try {
            return transportCatalogService.updateInfoById(convert(transportCatalogDTO));
        } catch (Exception e) {
            logger.error("TransportCatalog update by id failed.", e);
            throw new RuntimeException("TransportCatalog update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(TransportCatalogDTOQuery transportCatalogDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportCatalogDTOQuery.getW_id() != null && transportCatalogDTOQuery.getW_id() > 0) {
                query.put("w_id", transportCatalogDTOQuery.getW_id());
            }

            if (transportCatalogDTOQuery.getIds() != null && !transportCatalogDTOQuery.getIds().equals("")) {
                query.put("ids", transportCatalogDTOQuery.getIds());
            }

            if (transportCatalogDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportCatalogDTOQuery.getStopFlag());
            }

            if (transportCatalogDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportCatalogDTOQuery.getDeleteFlag());
            }

            return transportCatalogService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("TransportCatalog update by query failed.", e);
            throw new RuntimeException("TransportCatalog update by query failed, Please order your code.", e);
        }
    }

    @Override
    public TransportCatalogDTO selectById(Integer id) {
        try {
            return convert(transportCatalogService.selectById(id));
        } catch (Exception e) {
            logger.error("TransportCatalog select by id failed.", e);
            throw new RuntimeException("TransportCatalog select by id failed, Please order your code.", e);
        }
    }

    @Override
    public TransportCatalogDTO selectObjByQuery(TransportCatalogDTOQuery transportCatalogDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportCatalogDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportCatalogDTOQuery.getStopFlag());
            }

            if (transportCatalogDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportCatalogDTOQuery.getDeleteFlag());
            }

            return convert(transportCatalogService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("TransportCatalog select obj by query failed.", e);
            throw new RuntimeException("TransportCatalog select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<TransportCatalogDTO> getListForPage(TransportCatalogDTOQuery transportCatalogDTOQuery) {
        Query query = Query.page(transportCatalogDTOQuery.getPn(), transportCatalogDTOQuery.getPageSize());

        if (transportCatalogDTOQuery.getId() != null && transportCatalogDTOQuery.getId() > 0) {
            query.put("id", transportCatalogDTOQuery.getId());
        }

        if (transportCatalogDTOQuery.getIds() != null && !transportCatalogDTOQuery.getIds().equals("")) {
            query.put("ids", transportCatalogDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(transportCatalogDTOQuery.getLikeName())) {
            query.put("likeName", transportCatalogDTOQuery.getLikeName());
        }

        if (StringUtils.isNotBlank(transportCatalogDTOQuery.getShowDate())) {
            query.put("showDate", transportCatalogDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(transportCatalogDTOQuery.getStartDate())) {
            query.put("startDate", transportCatalogDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(transportCatalogDTOQuery.getEndDate())) {
            query.put("endDate", transportCatalogDTOQuery.getEndDate());
        }

        if (StringUtils.isNotBlank(transportCatalogDTOQuery.getOrderBy())) {
            query.put("orderBy", transportCatalogDTOQuery.getOrderBy());
        }

        if (transportCatalogDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", transportCatalogDTOQuery.getStopFlag());
        }

        if (transportCatalogDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", transportCatalogDTOQuery.getDeleteFlag());
        }

        List<TransportCatalogDTO> list = convert(transportCatalogService.selectByQuery(query));
        Page<TransportCatalogDTO> page = Page.from(transportCatalogDTOQuery.getPn(), transportCatalogDTOQuery.getPageSize(), list);
        page.setTotalItems(transportCatalogService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return transportCatalogService.deleteById(id);
        } catch (Exception e) {
            logger.error("TransportCatalog delete failed.", e);
            throw new RuntimeException("TransportCatalog delete failed, Please order your code.", e);
        }
    }
}